package org.xtense.rcp.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jface.viewers.TableViewer;
import org.xtense.data.DBConfig;
import org.xtense.data.DBManager;
import org.xtense.engine.IConstants;
import org.xtense.rcp.editors.util.TableEntry;
import org.xtense.rcp.views.QueryView;

;

public class DocTablelUtil {

	public static void createDocTable(TableViewer tv) {
		DBManager manager = new DBManager(DBConfig.getInstance(false, null));
		ResultSet res = manager.executeSqlQuery("select * from document");
		List<TableEntry> list = new ArrayList<TableEntry>();
		HashMap<String, Integer> docs = new HashMap<String, Integer>();
		int id;
		String name;
		try {
			if (res != null) {
				while (res.next()) {
					id = res.getInt(IConstants.Document_ID);
					name = res.getString(IConstants.Document_Name);
					list.add(new TableEntry(id, name, res
							.getString(IConstants.Document_StoredTime)));
					docs.put(name, id);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		QueryView.fillDocNameCombo(docs);
		tv.setInput(list);
	}
}
